#!/bin/bash

# 设置要检查的进程名称（可以是部分名称）
PROCESS_NAME="GameServer"

# 设置要排除的进程名称（如果有的话）
EXCLUDE_NAME="GameServer1"

# 设置启动进程的命令
START_COMMAND="./start.sh"

# 设置检查间隔时间（秒）
CHECK_INTERVAL=15

check_process() {
    ps aux | grep "$PROCESS_NAME" | grep -v "$EXCLUDE_NAME" | grep -v grep > /dev/null
}

while true
do
    # 检查进程是否存在
    if check_process
    then
        echo "$(date): $PROCESS_NAME is running"
        echo "Matching processes:"
        ps aux | grep "$PROCESS_NAME" | grep -v "$EXCLUDE_NAME" | grep -v grep
    else
        echo "$(date): $PROCESS_NAME is not running. Starting it now..."
        $START_COMMAND
        
        # 等待几秒钟
        sleep 5
        
        # 再次检查进程是否成功启动
        if check_process
        then
            echo "$(date): $PROCESS_NAME has been successfully started"
        else
            echo "$(date): Failed to start $PROCESS_NAME"
        fi
    fi

    # 等待指定的间隔时间
    sleep $CHECK_INTERVAL
done